﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Data;
using OF.Common;
using System.Configuration;
namespace OF.BLL.Addr
{
    /// <summary>
    /// 区域、商家接口实现
    /// </summary>
    public class Area : DAL.Area
    {
      
        DAL.School dal_school = new DAL.School();
        DAL.BuyerAddress dal_address = new DAL.BuyerAddress();
        DAL.Seller dal_seller = new DAL.Seller();

        #region 获取区域(参数:areaid) addr.arealist.get
        public string doGetAreaList(string areaid)
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = GetList("parentid=" + areaid);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("AreaList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetAreaList");
            }
            return result;
        }
        #endregion

        #region 根据区域获取学校(参数:areaid) addr.school.get
        public string doGetAreaSchoolList(string areaid)
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_school.GetList("areaid=" + areaid);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("AreaSchoolList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetAreaSchoolList");
            }
            return result;
        }
        #endregion

        #region 根据学校查询商家(参数:schoolid) user.sellerbyschool.get
        public string doGetSellerList(string schoolid,string areaid,string cityid,string shoptype,string sort,string pageid)
        {
            string result = "{\"SellerList\":[]}";
            string where="";
            string orderby="";
            int pageindex = Common.Utils.StrToInt(pageid, 1);
            int pagesize = 10;
            try
            {
                //验证参数
                if (string.IsNullOrEmpty(schoolid) || string.IsNullOrEmpty(areaid) || string.IsNullOrEmpty(cityid) || cityid == "0")
                    return result;
                if (areaid == "0" && schoolid=="0")
                {
                    where = " exists(select sellerid from SellerBelong where schoolid in(select id from School where areaid in (select id from Area where parentid=" + cityid + ")))";
                }
                else if (areaid != "0" && schoolid == "0")
                {
                    where = " exists(select sellerid from SellerBelong where schoolid in(select id from School where areaid=" + areaid + "))";
                }
                else if (areaid == "0" && schoolid != "0")
                {
                    where = " exists(select sellerid from SellerBelong b where schoolid =" + schoolid + " and Seller.id =b.sellerid)";
                }
                else {
                    where = " exists(select sellerid from SellerBelong b where schoolid =" + schoolid + " and Seller.id =b.sellerid)";
                }
       
                //判断商铺类型
                if (shoptype != "0" && !string.IsNullOrEmpty(shoptype))
                {
                    where += " and shoptype="+shoptype;
                }
                //判断排序类型SALES_LARGEST【销量最大】;EVALUATE_BEST【评价最好】;PAY_LOW【人均最低】
                if (!string.IsNullOrEmpty(sort))
                {
                    if (sort == Enmus.SORT_TYPE.EVALUATE_BEST.ToString())
                        orderby = "  goodrating desc";
                    else if (sort == Enmus.SORT_TYPE.PAY_LOW.ToString())
                        orderby = "  average_spend asc";
                    else if (sort == Enmus.SORT_TYPE.SALES_LARGEST.ToString())
                        orderby = "   amount desc";
                }
                else {
                    orderby = " id desc";
                }
                DataSet ds = dal_seller.GetPageList(where, orderby, pageindex, pagesize);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("SellerList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetSellerList");
            }
            return result;
        }
        #endregion

        #region 模糊搜索学校(参数:school) addr.school.search
        public string doGetSchoolList(string school)
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_school.GetList("shcoolname like '%" + school + "%' or shortname like '%" + school + "%'");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("SchoolList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetSchoolList");
            }
            return result;
        }
        #endregion

        #region 保存用户地址 addr.useraddr.add
        public string doSendAddress(string address, string buyerid)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                if (dal_address.Add(address, buyerid,"1") > 0)
                {
                    return Enmus.RETURN.SUC.ToString();
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doSendAddress");
            }
            return result;
        }
        #endregion

        #region 修改用户地址 addr.useraddr.modify
        public string doModifyAddress(string address, string state,string addressid)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                //设为默认
                if (state == "2")
                {
                    //取消旧的默认记录
                    dal_address.UpdateState(Convert.ToInt32(addressid), 2, 1);
                }
                if (dal_address.Update(address,state,addressid) > 0)
                {
                    return Enmus.RETURN.SUC.ToString();
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doModifyAddress");
            }
            return result;
        }
        #endregion

        #region 删除用户地址 addr.useraddr.delete
        public string doDeleteAddress(string addressid)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                if (dal_address.DeleteAddress(addressid))
                {
                    return Enmus.RETURN.SUC.ToString();
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doDeleteAddress");
            }
            return result;
        }
        #endregion

        #region 根据用户查询地址列表(参数:buyerid) addr.useraddrlist.get
        public string doGetUserAddress(string buyerid)
        {
            string result = "{\"UserAddress\":[]}";
            try
            {
                DataSet ds = dal_address.GetJsonList("buyerid=" + buyerid+" and state!=0 ");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("UserAddress", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetUserAddress");
            }
            return result;
        }
        #endregion

        #region 获取所有学校 addr.schoolall.get
        public string doGetSchoolAllList()
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_school.GetList("");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("AreaSchoolAllList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetAreaSchoolList");
            }
            return result;
        }
        #endregion

        #region 获取区域和区域下的学校(参数:areaid)

        public string getAreaSchoollist(string areaid)
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = GetAreaSchoolById(areaid);
                DataTable dt = ds.Tables[0];
                string json = "{\"AreaSchool\":[";
                if (ds.Tables[0] != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string _areaid = dt.Rows[i]["id"].ToString();
                        bool flg = false;
                        for (int z = i - 1; z >= 0; z--)
                        {
                            if (_areaid == dt.Rows[z]["id"].ToString())
                            {
                                flg = true;
                            }
                        }
                        if (flg == true) continue;

                        json += "{\"areaid\":\"" + dt.Rows[i]["id"] + "\",\"area\":\"" + dt.Rows[i]["area"] + "\",";
                        string schools = "[";
                        for (int j = i; j < dt.Rows.Count - 1; j++)
                        {
                            if (_areaid == dt.Rows[j]["id"].ToString())
                            {
                                string s= dt.Rows[j]["schoolid"].ToString();
                                if (dt.Rows[j]["schoolid"] == null || dt.Rows[j]["schoolid"].ToString()=="") break;
                                schools += "{\"schoolid\":\"" + dt.Rows[j]["schoolid"] + "\",\"schoolname\":\"" + dt.Rows[j]["shcoolname"] + "\"},";
                            }
                        }
                        if (schools.Length > 1) schools = schools.Substring(0, schools.Length - 1);
                        schools += "]";
                        json += "\"schools\":" + schools + "},";
                    }
                    json = json.Substring(0, json.Length - 1);
                }
                json += "]}";
                result = json;
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "getAreaSchoollist");
            }
            return result;
        }

        #endregion
    }
}
